from datetime import datetime

from flask import g, request
from flask_restful import Resource

from api.verify_token import auth
from backend_server import db
from data_base.user_table import User


class Login(Resource):
    method_decorators = {'get': [auth.login_required]}
    def get(self):
        # 使用 verify_password 中，检验成功后保存的用户信息
        token = g.user.generate_token()
        return {"access_token": token}

class Register(Resource):
    def post(self):
        try:
            user = User(**request.json)
            db.session.add(user)
            db.session.commit()
        except Exception as E:
            # print(type(E.args))
            print(E)
            return {"msg": "register failure", "error": f"{E.__cause__}"}
        # print(request.json)

        return {"code": 0, "msg": "register success", "data": request.json}
